package bestTeam.WalkieDoggie.client.Composites;


import org.cobogw.gwt.user.client.ui.Rating;

import bestTeam.WalkieDoggie.client.Facebook;
import bestTeam.WalkieDoggie.client.WalkieDoggie;

import com.google.code.gwteyecandy.Tooltip;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class HeaderComp extends Composite {
	private VerticalPanel verticalPanel;
	private HorizontalPanel horizontalPanel;
	private VerticalPanel verticalPanel_1;
	private Label lblWalkiedoggie;
	private Label lblWhollLetThe;
	private VerticalPanel verticalPanel_2;
	private HorizontalPanel horizontalPanel_2;
	private Anchor lblUsername;
	private Anchor hprlnkLogout;
	private HorizontalPanel horizontalPanel_1;
	private MenuBar menuBar;
	private MenuItem mntmHome;

	private MenuItem mntmTodoList;

	private MenuItem mntmMyWalkies;
	private MenuItem mntmHistory;
	private MenuItem mntmProfile;
	private Image logo;
	private Button logoButton;
	protected userProfileComp userComp;
	
	private int cnt_myWalkies=0;
	private int cnt_todoWalkies=0;

	public HeaderComp(){

		verticalPanel = new VerticalPanel();
		verticalPanel.setSpacing(5);
		initWidget(verticalPanel);
		verticalPanel.setSize("100%", "100%");

		horizontalPanel = new HorizontalPanel();
		verticalPanel.add(horizontalPanel);
		horizontalPanel.setWidth("100%");

		logo = new Image("thinlogo.png");
		logo.setSize("100%", "100%");

		logo.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				
				WalkieDoggie.clearAllRootPanels();
				
				RootPanel panel = RootPanel.get("mainComp");
				panel.add(new Label("Loading... please wait"));

				if(WalkieDoggie.loginInfo.isNewUser())
					WalkieDoggie.loadWalkieDoggie();
				else
					WalkieDoggie.loadWalkieDoggie(WalkieDoggie.loginInfo.getUserData().getId());
				
			}
		});		
		
		SimplePanel sp = new SimplePanel();
		String width = "" +(Window.getClientWidth()/2) + "px";
		sp.setSize(width, "100%");
		sp.add(logo);

		horizontalPanel.add(sp);
		horizontalPanel.setCellVerticalAlignment(logo, HasVerticalAlignment.ALIGN_MIDDLE);
		
		verticalPanel_1 = new VerticalPanel();
		horizontalPanel.add(verticalPanel_1);
		verticalPanel_1.setWidth("100%");

		horizontalPanel_2 = new HorizontalPanel();
		VerticalPanel verticalPanel_20 = new VerticalPanel();
		horizontalPanel.add(verticalPanel_20);
		horizontalPanel.setCellHorizontalAlignment(verticalPanel_20, HasHorizontalAlignment.ALIGN_RIGHT);
		horizontalPanel.setCellVerticalAlignment(verticalPanel_20, HasVerticalAlignment.ALIGN_BOTTOM);
		
		verticalPanel_2 = new VerticalPanel();
		horizontalPanel.add(verticalPanel_2);
		horizontalPanel.setCellVerticalAlignment(verticalPanel_2, HasVerticalAlignment.ALIGN_BOTTOM);
		horizontalPanel.setCellHorizontalAlignment(verticalPanel_2, HasHorizontalAlignment.ALIGN_RIGHT);
		verticalPanel_2.setSize("100%", "100%");

		hprlnkLogout = new Anchor("(logout)");
		verticalPanel_2.add(hprlnkLogout);
		hprlnkLogout.setHeight("100%");
		verticalPanel_2.setCellHorizontalAlignment(hprlnkLogout, HasHorizontalAlignment.ALIGN_RIGHT);
		hprlnkLogout.setHref(Facebook.getLogoutUrl());
		
		Image picture = new Image("https://graph.facebook.com/"+WalkieDoggie.loginInfo.getUserData().getId()+"/picture?type=normal");
				
		verticalPanel_2.add(picture);
		
		verticalPanel_2.setCellHorizontalAlignment(picture, HasHorizontalAlignment.ALIGN_RIGHT);
		
//		horizontalPanel_2 = new HorizontalPanel();
//		verticalPanel_2.add(horizontalPanel_2);
//		verticalPanel_2.setCellVerticalAlignment(horizontalPanel_2, HasVerticalAlignment.ALIGN_MIDDLE);
//		verticalPanel_2.setCellHorizontalAlignment(horizontalPanel_2, HasHorizontalAlignment.ALIGN_RIGHT);


		//boolean needToFillBasic	Details = WalkieDoggie.loginInfo.getIsnewUser();
		//String fillDetailsHeader = needToFillBasicDetails ? "Please fill address and phone" : "";

		lblUsername = new Anchor();
		lblUsername.setText("Hi, "+WalkieDoggie.loginInfo.getUserData().getNickName()+"! ");
		//lblUsername.setHref(WalkieDoggie.loginInfo.getUserData().getFBprofileLink());
		lblUsername.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				userComp = new userProfileComp(WalkieDoggie.loginInfo.getUserData(),true);
				userComp.setPopupPosition(WalkieDoggie.header_comp.getAbsoluteLeft()+300, WalkieDoggie.header_comp.getAbsoluteTop()+200);
				userComp.setGlassEnabled(true);
				userComp.show();
			}
		});

		//verticalPanel_2.add(lblUsername);
		lblUsername.setStyleName("handCursor");
		//verticalPanel_2.setCellHorizontalAlignment(lblUsername, HasHorizontalAlignment.ALIGN_RIGHT);
		lblUsername.setHeight("100%");

		Tooltip t = new Tooltip();
		t.setText("click for details");
		t.attachTo(lblUsername);
		
		//Label ranking = new Label("rank:"+WalkieDoggie.loginInfo.getUserData().getRanking());
		Rating rate = new Rating((int)WalkieDoggie.loginInfo.getUserData().getRanking(), 5, Rating.LTR, "iBonesmall.png","iBoneBWsmall.png",
				"iBonesmall.png", 22, 24);
		rate.setReadOnly(true);
		
		
		//verticalPanel_2.add(horizontalPanel_2);
		verticalPanel_2.setCellVerticalAlignment(horizontalPanel_2, HasVerticalAlignment.ALIGN_MIDDLE);
		verticalPanel_2.setCellHorizontalAlignment(horizontalPanel_2, HasHorizontalAlignment.ALIGN_RIGHT);
		Label rank = new Label(" your rank:");
		
		verticalPanel_20.add(lblUsername);
		verticalPanel_20.add(horizontalPanel_2);
		//horizontalPanel_2.add(lblUsername);
		horizontalPanel_2.add(rank);
		horizontalPanel_2.add(rate);
			
		Image help = new Image("qm.png");
		horizontalPanel_2.add(help);
		
		Tooltip t1 = new Tooltip();
		t1.setText("The Rank represents your grade as a walker. It's calculated by the feedback of walkers that you've volunteered to.");
		t1.attachTo(help);
				
		horizontalPanel_1 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_1);
		horizontalPanel_1.setWidth("100%");

		menuBar = new MenuBar(false);
		horizontalPanel_1.add(menuBar);
		menuBar.setWidth("100%");

		mntmHome = new MenuItem("Ongoing Walkies", false, new Command() {
			public void execute() {
				WalkieDoggie.mainPanel.clear();
				WalkieDoggie.mainPanel.add(WalkieDoggie.home_comp);
				WalkieDoggie.home_comp.getTabPanel().selectTab(0);
				WalkieDoggie.sidePanel.add(WalkieDoggie.side_comp);	
			}
		});
		menuBar.addItem(mntmHome);
		
		
		//mntmHome.addMouseOverHandler()

				mntmTodoList = new MenuItem("My ToDo list", false, new Command() {

					public void execute() {
		
						WalkieDoggie.mainPanel.clear();
						WalkieDoggie.mainPanel.add(WalkieDoggie.my_ToDo_comp);
						WalkieDoggie.sidePanel.add(WalkieDoggie.side_comp);
					
					}
				});

				menuBar.addItem(mntmTodoList);



		mntmMyWalkies = new MenuItem("My Walkies", false, new Command() {
			public void execute() {

				WalkieDoggie.mainPanel.clear();
				WalkieDoggie.mainPanel.add(WalkieDoggie.my_walkies_comp);
				WalkieDoggie.sidePanel.add(WalkieDoggie.side_comp);

			}
		});
		menuBar.addItem(mntmMyWalkies);

		mntmHistory = new MenuItem("History of Walkies", false, new Command() {
			public void execute() {

				WalkieDoggie.mainPanel.clear();
				WalkieDoggie.mainPanel.add(WalkieDoggie.history_comp);
				WalkieDoggie.sidePanel.add(WalkieDoggie.side_comp);

			}
		});
		menuBar.addItem(mntmHistory);
		
		mntmProfile = new MenuItem("profile", false, new Command() {
			public void execute() {
				WalkieDoggie.mainPanel.clear();
				WalkieDoggie.sidePanel.clear();
				WalkieDoggie.mainPanel.add(WalkieDoggie.profile_comp);
			}
		});

		menuBar.addItem(mntmProfile);
		
		Tooltip tt1 = new Tooltip();
		tt1.setText("click to refresh views");
		tt1.attachTo(logo);

	}
	public MenuItem getMntmTodo() {
		return mntmTodoList;
	}
	public void setMntmTodo(MenuItem mntmTodo) {
		this.mntmTodoList = mntmTodo;
	}
	public MenuItem getMntmHome() {
		return mntmHome;
	}
	public void setMntmHome(MenuItem mntmHome) {
		this.mntmHome = mntmHome;
	}
	public MenuItem getMntmMyWalkies() {
		return mntmMyWalkies;
	}
	public void setMntmMyWalkies(MenuItem mntmMyWalkies) {
		this.mntmMyWalkies = mntmMyWalkies;
	}
	public int getCnt_myWalkies() {
		return cnt_myWalkies;
	}


	public void setCnt_myWalkies(int cnt_myWalkies) {
		this.cnt_myWalkies = cnt_myWalkies;
	}


	public int getCnt_todoWalkies() {
		return cnt_todoWalkies;
	}


	public void setCnt_todoWalkies(int cnt_todoWalkies) {
		this.cnt_todoWalkies = cnt_todoWalkies;
	}

}
